﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace DTcms.SMS.Aliyun
{
    internal class AliYunSmsSign
    {
        public AliYunSmsSign(AliYunSmsSetting aliYunSmsSetting)
        {
            this._AliYunSmsSetting = aliYunSmsSetting;
        }

        public string GetApi(string phoneNumbers, string templateCode, string templateParamsJson)
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            string value = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.CreateSpecificCulture("en-US"));
            dictionary.Add("AccessKeyId", this._AliYunSmsSetting.AccessKeyId);
            dictionary.Add("Action", "SendSms");
            dictionary.Add("Format", "XML");
            dictionary.Add("OutId", "");
            dictionary.Add("PhoneNumbers", phoneNumbers);
            dictionary.Add("RegionId", "cn-hangzhou");
            dictionary.Add("SignName", this._AliYunSmsSetting.SignName);
            dictionary.Add("SignatureMethod", "HMAC-SHA1");
            dictionary.Add("SignatureNonce", Guid.NewGuid().ToString("N"));
            dictionary.Add("SignatureVersion", "1.0");
            dictionary.Add("TemplateCode", templateCode);
            dictionary.Add("TemplateParam", templateParamsJson);
            dictionary.Add("Timestamp", value);
            dictionary.Add("Version", "2017-05-25");
            bool flag = dictionary.ContainsKey("Signature");
            if (flag)
            {
                dictionary.Remove("Signature");
            }
            StringBuilder stringBuilder = new StringBuilder();
            foreach (KeyValuePair<string, string> keyValuePair in dictionary)
            {
                string key = keyValuePair.Key;
                stringBuilder.Append("&").Append(AliYunSmsSign.SpecialUrlEncode(key)).Append("=").Append(AliYunSmsSign.SpecialUrlEncode(keyValuePair.Value));
            }
            string text = stringBuilder.ToString().Substring(1);
            StringBuilder stringBuilder2 = new StringBuilder();
            stringBuilder2.Append("GET").Append("&");
            stringBuilder2.Append(AliYunSmsSign.SpecialUrlEncode("/")).Append("&");
            stringBuilder2.Append(AliYunSmsSign.SpecialUrlEncode(text));
            string value2 = this.SignString(this._AliYunSmsSetting.AccessKeySecret + "&", stringBuilder2.ToString());
            string str = AliYunSmsSign.SpecialUrlEncode(value2);
            return "http://dysmsapi.aliyuncs.com/?Signature=" + str + "&" + text;
        }

        public string SignString(string accessSecret, string source)
        {
            string result;
            using (KeyedHashAlgorithm keyedHashAlgorithm = KeyedHashAlgorithm.Create("HMACSHA1"))
            {
                keyedHashAlgorithm.Key = Encoding.UTF8.GetBytes(accessSecret.ToCharArray());
                result = Convert.ToBase64String(keyedHashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(source.ToCharArray())));
            }
            return result;
        }

        private static string SpecialUrlEncode(string value)
        {
            return HttpUtility.UrlEncode(value, Encoding.UTF8).Replace("+", "%20").Replace("*", "%2A").Replace("%7E", "~");
        }

        private AliYunSmsSetting _AliYunSmsSetting;
    }
}
